App({

    data: {
        service_url: 'https://recovery.xiaojiang.pub/api_wp_v01/',
        // service_url: 'https://xiaojiang.oicp.vip/api_wp_v01/',
        client_token: '',
        request_lock: true,
        login_lock: true,
        staff_uncertified_jump: '/pages/staff_home/unverified/index'
    },

    utils: {
        qqmap: undefined,
        qqmap_key: 'V6EBZ-KYWK4-QMMUD-D4NT4-LNK2T-FFBIW',
        qqmap_sk: 'iGxObJn8Wkj1AJqPddK8onTZrpI7Q8c8',
    },

    onLaunch: function() {
        /* 获取ClientToken */
        let app = this;
        wx.getStorage({
            key: 'client_token',
            success: function(response) {
                app.data.login_lock = false;
                app.data.client_token = response.data;
            },
            fail: function(e) {
                app.data.login_lock = false;
            }
        });

    },

    /**
     * 自定义函数--验证登录
     */
    verifyLogin: function() {

        let app = this;
        if (true === app.data.login_lock) {
            setTimeout(function() {
                app.verifyLogin();
            }, 200);
            return;
        };
        if (0 === app.data.client_token.length) {
            wx.showToast({
                title: '登录中...',
                icon: 'loading',
                mask: true,
                duration: 50000
            });
            wx.login({
                success: function(response) {
                    let data = {
                        code: response.code
                    };
                    wx.request({
                        url: app.data.service_url + 'index/subscriber_login',
                        data: data,
                        method: 'POST',
                        success: function(response) {
                            wx.hideToast();
                            if (200 === response.statusCode) {
                                if (0 === response.data.errno) {
                                    wx.setStorage({
                                        key: 'client_token',
                                        data: response.data.data.client_token
                                    });
                                    app.data.request_lock = false;
                                    app.data.client_token = response.data.data.client_token;
                                } else {
                                    wx.showToast({
                                        title: '登录失败，' + response.data.message,
                                        icon: 'none'
                                    });
                                };
                            } else {
                                wx.showToast({
                                    title: '登录失败，远程服务返回错误',
                                    icon: 'none'
                                });
                            };
                        },
                        fail: function() {
                            wx.hideToast();
                            wx.showToast({
                                title: '登录失败，连接远程服务失败',
                                icon: 'none'
                            });
                        }
                    });
                },
                fail: function() {
                    wx.hideToast();
                    wx.showToast({
                        title: '登录失败，微信Code未获取',
                        icon: 'none'
                    });
                }
            });
        } else {
            this.data.request_lock = false;
        };
    },

    /**
     * 自定义函数--在登录完成后执行默认操作
     */
    execute: function(execute_function) {

        let app = getApp();
        if (false !== app.data.request_lock) {
            setTimeout(function() {
                app.execute(execute_function);
            }, 500);
        } else {
            execute_function();
        };

    },

    /**
     * 自定义函数--初始化QQ地图
     */
    initQQmap: function() {

        if (undefined === this.utils.qqmap) {
            let qqmap_file = require('/utils/qqmap-wx-jssdk.js');
            this.utils.qqmap = new qqmap_file({
                key: this.utils.qqmap_key
            });
            return this.utils.qqmap;
        } else {
            return this.utils.qqmap;
        };

    },

    /**
     * 自定义函数--获取页面
     */
    getPage: function() {

        let pages = getCurrentPages();
        return pages[pages.length - 1];

    }

});